<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>配置供应商路由</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script type="text/javascript" src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
    <script type="text/javascript" src="../../js/commonutil.js"></script>
    <script src="./function.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/jquery.min.js" charset="utf-8"></script>
</head>
<body>
<div class="layui-container">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>历史路由配比参数</legend>
    </fieldset>
    <table class="layui-hide" id="supplierId"></table>
    <br/><br/>
    <hr class="layui-bg-blue">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
        <legend>设置路由配比参数(百分比)</legend>
    </fieldset>

    <input type="text" id="channelNo" value="" style="display:none">
    <form class="layui-form" id="addTemplateForm">
        <div class="layui-form-item">
            <div class="layui-row">
                <label class="layui-form-label">中金</label>
                <div class="layui-input-inline">
                    <input type="text" id="supplier-zj" name="zj"
                           lay-verify="required|templateNoVerify"
                           style="width: 170px" autocomplete="off" placeholder="" maxlength="50" class="layui-input">
                </div>
                <label class="layui-form-label">法大大</label>
                <div class="layui-input-inline">
                    <input type="text" id="supplier-fdd" name="fdd"
                           lay-verify="required|templateNoVerify"
                           style="width: 170px" autocomplete="off" placeholder="" maxlength="50" class="layui-input">
                </div>
                <label class="layui-form-label">懒猫</label>
                <div class="layui-input-inline">
                    <input type="text" id="supplier-lm" name="lanmao"
                           lay-verify="required|templateNoVerify"
                           style="width: 170px" autocomplete="off" placeholder="" maxlength="50" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <textarea placeholder="请输入内容" name="remark" id="remark" style="width: 790px" maxlength="200"
                      lay-verify="remarksVerify"
                      class="layui-textarea"></textarea>
        </div>
        <div class="layui-form-item">
            <div class="btnclass" style="padding-left: 350px; padding-top: 20px;">
                <div class="layui-btn" lay-submit="" autocomplete="off" onclick="save()">确认生效</div>
                <div id="closeWin" class="layui-btn layui-btn-primary">关闭页面</div>
            </div>
        </div>

    </form>
</div>
</body>
<script>
    layui.use(['laypage', 'jquery', 'layer', 'table', 'form', 'util', 'upload', 'layedit'], function () {
        var id = GetUrlParam("id");
        var url = serverPre + "/manager/scs/v2/productSign/info?productCode=" + id;
        var table = layui.table;
        var channelVo;
        //第一个实例
        table.render({
            elem: '#supplierId',
            url: url,
            page: false,
            method: 'POST',
            height: '200',
            headers: {"token": getCookie("token")},
            contentType: "application/json",
            cols: [[
                {type: 'numbers', align: 'center', width: '10'}
                , {field: 'channelNo', title: '产品编号', width: '180', align: 'center'}
                , {field: 'productCode', title: '产品代码', width: '100', align: 'center'}
                , {field: 'channelName', title: '产品名称', width: '130', align: 'center'}
                , {field: 'productSignStr', title: '路由配比', width: '250', align: 'center'},
                , {field: 'tenant', title: '所属企业', width: '200', align: 'center'}
                , {
                    field: 'effectiveTime', title: '路由生效时间', width: '170', align: 'center', templet: function (d) {
                        return toDateString(d.effectiveTime);
                    }
                }
            ]],
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.pageInfo.totalCount, //解析数据长度
                    "pageNo": res.data.pageInfo.pageNo, //解析数据长度
                    "pageSize": res.data.pageInfo.pageSize, //解析数据长度
                    "data": res.data.pageInfo.list, //解析数据列表
                    "data2": res.data.channelVo //解析数据列表
                };
            }
            , done: function (res) {
                var channelId = res.data2.channelNo;
                $('#channelNo').val(channelId);
            }
            , error: function () {
                layer.close(loading);
            }
        });

    })

    function save() {
        var remark = $("#remark").val();
        var channelNo = $("#channelNo").val();
        var lanmao = $("#supplier-lm").val();
        var zj = $("#supplier-zj").val();
        var fdd = $("#supplier-fdd").val();
        // if (isBlank(channelNo)) {
        //     alert("产品代码不能为空")
        //     return;
        // }
        if (isBlank(lanmao) || isBlank(zj)) {
            alert("配比不能为空")
            return;
        }
        if (Number(zj) + Number(lanmao) + Number(fdd) != 100) {
            alert("配比之和必须等于100%")
            return;
        }
        var url = serverPre + "/manager/scs/v2/productSign/save";
        var productRoList = new Array();
        var lanmaoRo = new Object();
        lanmaoRo.productSign = "lanmaoSignature";
        lanmaoRo.productName = "懒猫";
        lanmaoRo.percentage = lanmao;
        productRoList.push(lanmaoRo);

        var zjRo = new Object();
        zjRo.productSign = "zjSignature";
        zjRo.productName = "中金";
        zjRo.percentage = zj;
        productRoList.push(zjRo);

        var fddRo = new Object();
        fddRo.productSign = "fddSignature";
        fddRo.productName = "法大大";
        fddRo.percentage = fdd;
        productRoList.push(fddRo);

        let commitData = JSON.stringify({
            "productCode": channelNo,
            "remark": remark,
            "productRoList": productRoList
        });

        $.ajax({
            type: 'POST',
            contentType: 'application/json',
            headers: {'token': getCookie('token')},
            url: url,
            data: commitData,
            success: function (res) {
                if (res.code === 0) {
                    layui.use('layer',function(){
                        let layer = layui.layer;
                        layer.msg("保存成功!",{
                            icon:1,
                            time:2000,
                            end: function () {
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index);
                                parent.location.reload();
                            }
                        });
                    });
                }
            },
            error: function (err) {
                errorMsg("保存失败")
            }
        });
    }
</script>
<script>
    //取消
    $("#closeWin").click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    function isNotBlank(s) {
        return s != undefined && s != null && (s + "").trim().length > 0
    }

    function isBlank(s) {
        return !isNotBlank(s);
    }
</script>
</html>
